/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package hoan.com.test.service.impl;

import hoan.com.test.model.Book;
import hoan.com.test.service.BookLocalServiceUtil;
import hoan.com.test.service.base.BookLocalServiceBaseImpl;
import hoan.com.test.service.persistence.BookFinderUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.persistence.UserUtil;

/**
 * The implementation of the book local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link hoan.com.test.service.BookLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author hoan.nguyen
 * @see hoan.com.test.service.base.BookLocalServiceBaseImpl
 * @see hoan.com.test.service.BookLocalServiceUtil
 */
public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * hoan.com.test.service.BookLocalServiceUtil} to access the book local
	 * service.
	 */

	/**
	 * 
	 */
	public Book addBook(long userId, String title) throws NoSuchUserException,
			SystemException {

		User user = UserUtil.findByPrimaryKey(userId);
		Date now = new Date();
		long bookId = CounterLocalServiceUtil.increment(Book.class.getName());

		Book book = bookPersistence.create(bookId);

		book.setTitle(title);
		book.setCompanyId(user.getCompanyId());
		book.setUserId(user.getUserId());
		book.setUserName(user.getFullName());
		book.setCreateDate(now);
		book.setModifiedDate(now);
		book.setTitle(title);

		return bookPersistence.update(book, false);
	}

	/**
	 * 
	 * @param userId
	 * @return
	 */
	public List<Book> findAllBooks() {

		List<Book> bookList = new ArrayList<Book>();

		try {
			bookList = bookPersistence.findAll();
		} catch (SystemException e) {
			e.printStackTrace();
		}

		return bookList;
	}
	
	/**
	 * 
	 * @param userId
	 * @return
	 */
	public List<Book> findAllBooksByUserId(long userId) {
		
		return BookFinderUtil.findAllBooksByUserId(userId);
	}
}